package com.b511.swpu.cementbackend.repository;

import com.b511.swpu.cementbackend.dto.output.CompanyNameDto;
import com.b511.swpu.cementbackend.dto.output.DepartmentNameDto;
import com.b511.swpu.cementbackend.entity.MAIN_Organization;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MAIN_OrganizationRepository extends JpaRepository<MAIN_Organization, String>,
        JpaSpecificationExecutor<MAIN_Organization> {

    void deleteByRelationCode(String relationCode);

    MAIN_Organization findByRelationCode(String relationCode);

    @Query(value = "select new com.b511.swpu.cementbackend.dto.output.CompanyNameDto(a.name,a.code) from MAIN_Organization a where a.layer=:layer")
    List<CompanyNameDto> findCompanyName(@Param("layer") String layer);

    @Query(value = "select new com.b511.swpu.cementbackend.dto.output.DepartmentNameDto(a.name,a.code) from MAIN_Organization a where a.layer=:layer")
    List<DepartmentNameDto> findDepartmentName(@Param("layer") String layer);

    @Query(value = "select parentcode from main_organization where code =:code",nativeQuery = true)
    String findParentCode(@Param("code") String code);

    @Query(value = "select name from main_organization where code =:code",nativeQuery = true)
    String findName(@Param("code") String code);
}
